<?php
/**
 * Co 5 function chinh nhu phia duoi day
 * function &instance() dung de khoi tao
 *
 * Cach su dung de goi cac ham trong class nay like::ten_function();
 * Khong can su dung new nua.
 * **/
class like{

	/**
	 * function nay tu dong duoc su dung de khoi tao;
	 * */
	function &instance(){
		static $like;
		if (empty($like)){
			$like = new like();
		}
		return $like;
	}

	/**
	 * @author Pham
	 * @param
	 *
	 * $itemID is id of item
	 * $itemModule id module of item
	 *
	 * @return tra ve gia tri true hoac false
	 * */
	function likeItem($itemID, $itemModule){
		global $_G;
		$status = true;
		$data = array();
		$str = $idtype = $title = '';

		if (!$_G['uid'])
			return false;

		// Check $itemID exists
		switch ($itemModule){
			case 'product':
				$spaceuid = 99;
				$idtype = 'pid';

				$res = DB::query('SELECT pid, pname, uid FROM ' . DB::table('shop_products') . " WHERE pid = '$itemID'");
				while ($row = DB::fetch($res)){
					$pid = $row['pid'];
					$pname = $row['pname'];
					$puid = $row['uid'];
				}
			break;
		}

		if (empty($idtype) || empty($pid)){
			$str = lang('shop/tempalte', 'favorite_cannot_favorite');
			$status = false;
		}

		// Kiem tra xem user da like san pham nay chua
		if (DB::result_first('SELECT * FROM ' . DB::table('home_favorite') . " WHERE uid='$_G[uid]' AND idtype='$idtype' AND id='$pid'")){
			$str = lang('shop/template', 'favorite_repeat');
			$status = false;
		}

		// Neu thoa man dk thi like thoi ^^
		if ($pid && $status == true){
			$arr = array(
					'uid'=>intval($_G['uid']),
					'idtype'=>$idtype,
					'id'=>$pid,
					'spaceuid'=>$spaceuid,
					'title'=>$pname,
					'description'=>'',
					'dateline'=>TIMESTAMP
			);
			DB::insert('home_favorite', $arr);

			// Update total like
			switch ($itemModule){
				case 'product':
					DB::query('UPDATE ' . DB::table('shop_products') . " SET plike=plike+1 WHERE pid = $pid");

					if ($_G['uid'] != $puid){
						// Gui thong bao cho nguoi dang san pham
						$array = array(
								'actor'=>$_G['username'],
								'pname'=>$pname,
								'url'=>'shop.php?mod=view&name=' . locdau($pname) . '&pid=' . $pid,
						);
						notification_add($puid, 'likeproduct', 'like_product', $array, 1);
					}
				break;
			}

			$str = 'liked';
		}

		return $str;
	}

	/**
	 * @author Pham
	 * @param
	 *
	 * $itemID is id of item
	 * $itemModule id module of item
	 *
	 * @return tra ve gia tri true hoac false
	 * */
	function unLikeItem($itemID, $itemModule){
		global $_G;
		$status = true;
		$str = $idtype = '';
		$pid = 0;

		if (!$_G['uid'])
			return false;

		// Check $itemID exists
		switch ($itemModule){
			case 'product':
				$idtype = 'pid';
				$pid = DB::result_first('SELECT pid FROM ' . DB::table('shop_products') . " WHERE pid = '$itemID'");
			break;
		}

		if (empty($idtype) || empty($pid)){
			$str = lang('shop/tempalte', 'favorite_cannot_favorite');
			$status = false;
		}
		// Thoa man dk thi like thoi ^^
		if ($pid && $status == true){
			// downgrate total like
			switch ($itemModule){
				case 'product':
					// Kiem tra xem user da like san pham nay chua
					if (DB::result_first('SELECT * FROM ' . DB::table('home_favorite') . " WHERE uid='$_G[uid]' AND idtype='$idtype' AND id='$itemID'")){
						DB::query("UPDATE " . DB::table('shop_products') . " SET plike=plike-1 WHERE pid = '$itemID'");
						DB::delete("home_favorite", "uid='$_G[uid]' AND idtype='$idtype' AND id='$itemID'");
					}
				break;
			}

			$str = 'unliked';
		}

		return $str;
	}

	/**
	 * @author Trieu
	 *
	 * $itemID is id of item
	 * $idtype id module of item in `home_favorite` table
	 *
	 * @return true or false;
	 * */
	function checkLike($itemID, $idtype){
		global $_G;

		if (!$itemID)
			return false;

		if (DB::result_first('SELECT * FROM ' . DB::table('home_favorite') . " WHERE uid='$_G[uid]' AND idtype='$idtype' AND id='$itemID'"))
			return true;
		else
			return false;
	}

	/**
	 * @author Pham
	 * @param
	 *
	 * $itemID is id of item
	 * $itemModule id module of item
	 *
	 * @return tra ve mot array list nhung thanh vien da like
	 * */
	function ListLikeItem($itemID, $itemModule){
		global $_G;
		$list = array();

		if (!$itemID)
			return false;

		$Qry = 'SELECT f.uid, m.username, p.realname, p.bio
				FROM ' . DB::table('home_favorite') . ' f
				LEFT JOIN ' . DB::table('common_member') . ' m ON (m.uid = f.uid)
				LEFT JOIN ' . DB::table('common_member_profile') . ' p ON (m.uid = p.uid)
				WHERE f.id = ' . $itemID . '
				GROUP BY uid
				ORDER BY dateline ASC';

		$res = DB::query($Qry);
		while ($row = DB::fetch($res)){
			$list[] = $row;
		}

		return $list;
	}

	/**
	 * @author Pham
	 * @param
	 *
	 * $itemID is id of item
	 * $itemModule id module of item
	 *
	 * @return Tra ve mot so nguyen kieu int thong bao so luong like
	 * */
	function totalLikeItem($itemID, $itemModule){
		global $_G;
		$count = 0;

		if (!$itemID)
			return false;

		$count = getcount('home_favorite', "uid='$_G[uid]' AND idtype='$idtype' AND id='$itemID'");

		return $count;
	}
}